#include "../head/page3.h"
#include "../head/page1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

lv_obj_t *img_container = NULL;


// 添加图片
void add_image(lv_obj_t *container, const char *path, const char *name) {
    // 创建父容器
    lv_obj_t *img_box = lv_obj_create(container);
    lv_obj_set_size(img_box, 240, 360);  
    lv_obj_set_flex_flow(img_box, LV_FLEX_FLOW_COLUMN); // 垂直排列
    lv_obj_set_style_pad_all(img_box, 5, 0);
    lv_obj_clear_flag(img_box, LV_OBJ_FLAG_SCROLLABLE); // 不让容器自己滚动

    // 创建名字标签
    lv_obj_t *label = lv_label_create(img_box);
    lv_label_set_text(label, name);
    lv_obj_set_style_text_font(label, chinese_font, 0);
    lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);

    // 创建图片
    lv_obj_t *img = lv_img_create(img_box);
    lv_img_set_src(img, path);
    lv_obj_set_size(img, 240, 320);
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);

    // 点击事件，传递名字
    char *name_copy = malloc(strlen(name) + 1);
    strcpy(name_copy, name);
    lv_obj_add_event_cb(img, img_click_event_cb4, LV_EVENT_CLICKED, name_copy);
}


// 图片点击回调
void img_click_event_cb4(lv_event_t *e) {
    char *name = lv_event_get_user_data(e);
    if(name) {
        lv_textarea_set_text(ta2, name);
        free(name);  // 点击一次就释放
    }
    lv_scr_load(page1_prev);
}



int set_file_flag(const char *filename,int newflag){
    FILE *fp = fopen("/picture/picture.txt", "r+");
    if(fp == NULL){
        perror("打开文件失败");
        return -1;
    }
    char lines_temp[100][128];
    int count_temp = 0;
    char filename_temp[50];
    char name_temp[50];
    int flag_temp;
    int found=0;
    while(fscanf(fp, "%s %s %d", filename_temp, name_temp, &flag_temp) == 3){
        if(strcmp(name_temp,filename)==0){
            flag_temp=newflag;
            found=1;
        }
        snprintf(lines_temp[count_temp], sizeof(lines_temp[count_temp]), "%s %s %d\n", filename_temp, name_temp, flag_temp);
        count_temp++;
    }
    fclose(fp);

      if (!found) {
        // 文件中没找到传入的文件名
        return 1;
    }


    fp = fopen("/picture/picture.txt", "w");
    if(fp == NULL){
        perror("打开文件失败");
        return -1;
    }
    for(int i = 0; i < count_temp; i++){
        fputs(lines_temp[i], fp);
    }
    fclose(fp);
    printf("更新 %s 的标志位为 %d\n ",filename,newflag);
    return 0;
}

// 创建 page3
void create_page3(void) {
    page1_prev = lv_scr_act();   // 保存前一页面

    page3 = lv_obj_create(NULL);
    lv_scr_load(page3);

    // 横向滚动容器
    img_container = lv_obj_create(page3);
    lv_obj_set_size(img_container, LV_HOR_RES, LV_VER_RES - 60);
    lv_obj_align(img_container, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_scroll_dir(img_container, LV_DIR_HOR);
    lv_obj_set_scrollbar_mode(img_container, LV_SCROLLBAR_MODE_AUTO);

    lv_obj_set_layout(img_container, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(img_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(img_container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(img_container, 5, 0);

    // 读取图片文件名
    FILE *fp = fopen("/picture/picture.txt","r");
    if(fp == NULL){
        perror("打开文件失败");
        return;
    }

    char name[50];
    char filename[50];
    char fullpath[100];
    int flag=0;
    while (fscanf(fp, "%s %s %d", filename, name, &flag) == 3) {
        if(flag){
            snprintf(fullpath, sizeof(fullpath), "/picture/%s", filename);
            add_image(img_container, fullpath, name);
            printf("文件: %s, 站名: %s\n", fullpath, name);
        }else{
            printf("文件: %s, 站名: %s, 跳过\n", fullpath, name);
        }
        flag=0;
    }
    fclose(fp);

    // 返回按钮
    lv_obj_t *btn = lv_btn_create(page3);
    lv_obj_set_size(btn, 100, 50);
    lv_obj_align(btn, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
    lv_obj_t *label1 = lv_label_create(btn);
    lv_label_set_text(label1, "返回");
    lv_obj_set_style_text_font(label1, chinese_font, 0);
    lv_obj_center(label1);
    lv_obj_add_event_cb(btn, return_main_page2, LV_EVENT_CLICKED, NULL);
}
